﻿using B.S.BaseData.suyuan.ApiRead.Account.Command;
using B.S.BaseData.suyuan.ApiRead.DTO;
using B.S.BaseData.suyuan.InforStances.Interface;
using B.S.BaseData.Suyuan.Command;
using B.S.BaseData.Suyuan.Dommain;
using MediatR;
using static System.Net.Mime.MediaTypeNames;

namespace B.S.BaseData.suyuan.ApiRead.Account.CommandHeader
{
    public class GetrabbitCommandHeader : IRequestHandler<GetrabbitCommand, ApiResult<ApiPaging<GetQuanDto>>>
    {
        private readonly IBaseRepository<JiBeninformation> jibenBase;
        private readonly IBaseRepository<ShopMondel> ShopBase;

        public GetrabbitCommandHeader(IBaseRepository<JiBeninformation> jibenBase, IBaseRepository<ShopMondel> shopBase)
        {
            this.jibenBase = jibenBase;
            this.ShopBase = shopBase;
        }

        public Task<ApiResult<ApiPaging<GetQuanDto>>> Handle(GetrabbitCommand request, CancellationToken cancellationToken)
        {
            ApiResult<ApiPaging<GetQuanDto>> result=new ApiResult<ApiPaging<GetQuanDto>>();
            result.Code = APIEnum.显示成功;
            result.Msg=APIEnum.显示成功.ToString();
            var list = from a in jibenBase.IQueryEntity().Result
                       join b in ShopBase.IQueryEntity().Result on a.ShopName equals b.Id
                       where a.Isdel == 1
                       select new GetQuanDto
                       {
                           Id = a.Id,
                           ShopName = a.ShopName,
                           batch = a.batch,
                           ShelfLife= a.ShelfLife,
                           ProductionLicense=a.ProductionLicense,
                           Manufacturers = a.Manufacturers,
                           image = a.image,
                           completeness = a.completeness,
                           CreateName = a.CreateName,
                           CreateTime=DateTime.Now,
                           ShopNames = b.ShopName,
                       };
            if (!string.IsNullOrEmpty(request.ShopName))
            {
                list = list.Where(x => x.ShopNames.Contains(request.ShopName));
            }
                int TotalCount=list.Count();
                int TotalPage = (int)Math.Ceiling(TotalCount * 1.0 / request.size);
            list = list.Skip((request.index - 1) * request.size).Take(request.size);
            ApiPaging <GetQuanDto> page=new ApiPaging<GetQuanDto>();
            page.TotalPage=TotalPage; 
            page.TotalCount=TotalCount;
            page.Items = list.ToList();
           result.data = page;
            return Task.FromResult(result);
        }
    }
}
